var dialog = {
  visible: false,
  type: 0,
  phone: '',
  show: function(type){
    this.type = type
    this.visible = true;
    this.render()
    $('#dialog').show();
    return this;
  },
  hide: function(){
    this.type = ''
    this.visible = false;
    this.render()
    $('#dialog').hide()
    return this;
  },
  
  render: function(){
    this.phone = ''
    var type = parseInt(this.type)
    var subColumn = page.data.bgs[9].subColumn
    if (type in subColumn) {
      var html = ''
      var bg = subColumn[type].typePicture[0].picPath3
      var btn = subColumn[5].typePicture[0].picPath3
      var btnGroup = []
      var positionTop = 450
      $('#dialog').css('background-image', 'url(' + bg + ')')
      if (type == 1) { // 积分不足
        btnGroup = [1, 0]
      } else if (type == 3) { // 具备兑换 
        btnGroup = [2, 0]
      } else if (type == 4) { // 兑换成功
        btnGroup = [2]
        positionTop = 580
      } else if (type == 2) { // 手机号错误
        btnGroup = [2]
      }

      if (btnGroup.length) {
        html += '<div class="item-group" style="top: ' + positionTop + 'px;">'
        for (var i = 0; i < btnGroup.length; i++) {
          var className = ''
          if (i == 0) {
            className = ' focus'
          }
          html += '<div style="background-image:url(' + btn + ')" id="btn-' + i + '" class="item item-' + btnGroup[i] + className + '"></div>'
        }
        html += '</div>' 
        if (dialog.type == 4) {
          html += this.createQrcode()
          html += '<div class="input"></div>'
        }
        $('#dialog').html(html)
      }
    }
  },

  createQrcode: function () {
    if (this.img) return this.img
    var platform = config.getPlatform().toUpperCase()
    var operators = config.getOperators().toUpperCase()
    var userid = config.userid
    var activity = config.activity
    var appkey = activity.appkey
    var aesKey = activity.aesKey
    var activityCode = activity.activityCode
    var zx = activity.zx
    var goodsCode = page.data.goodsDetail.code
    var orderInfo = userid + goodsCode
    var fulltime = this._getCurrentTime()
    var params = [operators, platform, userid, activityCode, goodsCode, orderInfo, fulltime]
    var param = encryption(params.join('_'), aesKey)
    var url = ''
    if (operators == 'YD') {
      url = 'http://192.168.129.130:10117/'
    } else if (operators == 'LT') {
      url = 'http://10.0.2.46:18080/'
    } else if (operators == 'DX') {
      url = 'http://192.168.6.176:18080/'
    }
    url += 'third/thirdpart/qrcode/acbind2?appkey=' + appkey + '&param=' + param + '&zx=' + zx
    this.img = '<img class="qrcode" src="' + url + '"/>'
    return this.img
  },

  _getCurrentTime: function () {
    var dateTime = new Date()
    var year = dateTime.getFullYear()
    var month = dateTime.getMonth() + 1
    month = month >= 10 ? month : '0' + month
    var date = dateTime.getDate()
    date = date >= 10 ? date : '0' + date
    var hour = dateTime.getHours()
    hour = hour >= 10 ?  hour : '0' + hour
    var minutes = dateTime.getMinutes()
    minutes = minutes >= 10 ?  minutes : '0' + minutes
    var second = dateTime.getSeconds()
    second = second >= 10 ?  second : '0' + second
    return year + month + date + hour + minutes + second
  },

  renderInput: function(value){
    if (this.phone.length >= 11) {
      return
    }
    this.phone += value
    $('#dialog .input').html(this.phone)
  },

  checkPhone: function () {
    var reg = /^(1)\d{10}$/;
    return reg.test(dialog.phone)
  }
}